package com.ccc.dingtalk.controller.db;

import com.ccc.dingtalk.pojo.db.BaseDO;
import com.ccc.dingtalk.response.ResponseCode;
import com.ccc.dingtalk.response.ResponseResult;
import com.ccc.dingtalk.service.db.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 通用请求处理控制器
 *
 * @author superC
 * @version 1.0.0
 * @date 2023/8/13
 */

@SuppressWarnings("all")
public class BaseController<T extends BaseDO, S extends IBaseService<T>> {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected S service;

    /**
     * 获取全部数据
     */
    @GetMapping("/all")
    public ResponseResult all() {
        try {
            return ResponseResult.success("获取数据成功", service.findAll());
        } catch (Exception e) {
            logger.error("【捕获异常-获取数据操作】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_GET_ERROR, e.getMessage());
        }
    }

    /**
     * 获取一条数据
     */
    @GetMapping("/get")
    public ResponseResult get() {
        try {
            return ResponseResult.success("获取数据成功", service.getOne());
        } catch (Exception e) {
            logger.error("【捕获异常-获取数据操作】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_GET_ERROR, e.getMessage());
        }
    }

    /**
     * 添加
     */
    @PostMapping()
    public ResponseResult insert(@RequestBody T entity) {
        try {
            return ResponseResult.success("添加数据成功", service.insert(entity));
        } catch (Exception e) {
            logger.error("【捕获异常-添加数据操作】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_ADD_FAILURE, e.getMessage());
        }
    }

    /**
     * 编辑
     */
    @PutMapping()
    public ResponseResult update(@RequestBody T entity) {
        try {
            return ResponseResult.success("修改数据成功", service.update(entity));
        } catch (Exception e) {
            logger.error("【捕获异常-修改数据操作】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_UPDATE_FAILURE, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public ResponseResult delete(@RequestBody T entity) {
        try {
            if (entity.getId() == null) {
                throw new IllegalArgumentException("entity ID must not be null");
            }
            service.delete(entity.getId());
            return ResponseResult.success("删除数据成功");
        } catch (Exception e) {
            logger.error("【捕获异常-删除数据操作】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_DELETE_FAILURE, e.getMessage());
        }
    }

}
